package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/admin/category")
@Slf4j
@Api(tags = "分类相关接口")
@RestController
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     * @return
     */
    @PostMapping
    @ApiOperation("新增分类")
    public Result  createCategory(@RequestBody CategoryDTO categoryDTO){
        categoryService.insert(categoryDTO);
        log.info("qingciucanshu:{}",categoryDTO);
        return Result.success();
    }

    /**
     * 分类分页条件查询
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页条件查询")
    public Result pageSelect(CategoryPageQueryDTO categoryPageQueryDTO){
       PageResult pageResult=categoryService.pageSelect(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 状态修改
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用禁用分类")
    public Result categoryStatus( @PathVariable Integer status, Long id){
        log.info("stasus和id:{},{}",status,id);
        categoryService.statusManage(status,id);
        return Result.success();
    }

    /**
     * 修改分类
     * @param categoryDTO
     * @return
     */
    @ApiOperation("修改分类")
    @PutMapping
    public Result updateCategory(@RequestBody CategoryDTO categoryDTO){
        log.info("修改分类:{}",categoryDTO);
        categoryService.update(categoryDTO);
        return Result.success();
    }

    /**
     *根据分类查询查询
     * @param categoryPageQueryDTO
     * @return
     */
    @ApiOperation("查询数据")
    @GetMapping("/list")
    public Result<List<Category>>select(CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("查询菜品参数:{}",categoryPageQueryDTO);
        List<Category> list= categoryService.select(categoryPageQueryDTO);
        return Result.success(list);
    }

    /**
     * 删除数据
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除分类")
    public Result delete(Long id){
        categoryService.deleteById(id);
        log.info("id:{}",id);
        return Result.success();
    }
}
